<?php
	class UNIVERSE extends CI_Controller{
		function  __construct() {
			parent::__construct();
			$this->load->model('Laporan_model');
			$this->load->model('bird_model','BIRD');
			$this->load->model('mamal_model','MAMAL');
			$this->load->model('reptil_model','REPTIL');
			$this->load->model('User_model');
			$this->load->model('Cabang_model');
			$this->load->model('Penjualan_model');
			
			$flood = "";
			
			$this->load->helper(array('html','form', 'url','jq_plugin'));
			$this->load->library(array('form_validation','pagination'));
		}
		function index(){
			$this->BimaSakti();
		}
		//line memberikan akses untuk penggunaan UNIVERSE
		function BimaSakti(){
			if($_POST){
				$this->flood = $this->MAMAL->cat($this->input->post('username'),$this->input->post('password'));
				if($this->flood == true){
					//$this->earth($this->MAMAL->cheetah());
					redirect('UNIVERSE/earth/'.$this->MAMAL->cheetah());
					//echo "helo";
				}else{
					
				$data['msg'] = "Sorry KeyPass Invalid!";
				}
			}else{
				$data['msg'] = "";
			}
			$this->load->view('admin_views/universe/login',$data);
		}
		//line universe Request Bu Beth 29juli2013
		function earth($id){
			if($_POST) {
				$data['default'] = $this->BIRD->Pigeon($this->input->post('week'));
				$data['week'] = $this->input->post('week');
				$this->load->view('admin_views/universe/page_keuangan_mingguan',$data);
			}else{
				$jpenjualan = $this->Laporan_model->selectJPenjualan();
				foreach ($jpenjualan as $g)
				{
					$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
					$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
				}
				
				if($id != $this->MAMAL->cheetah()){
					redirect('UNIVERSE/BimaSakti');
				}
				$week = $this->Laporan_model->selectWeek();
				foreach ($week as $g)
				{
					$data['id'][]	 	    = $g->id;
					$data['intid_week'][] 	= $g->intid_week;
				}
				$data['sidebar'] = $this->REPTIL->Snake($id);
				$data['key'] = $id;
				$this->load->view('admin_views/universe/form_keuangan_mingguan',$data);
				
			}
		}
		//line universe Request Bu Beth 07Mei2013
		/*function earth($id){
			if($_POST) {
				$data['default'] = $this->BIRD->Pigeon($this->input->post('week'));
				$data['week'] = $this->input->post('week');
				$this->load->view('admin_views/universe/page_keuangan_mingguan',$data);
			}else{
				
				if($id != $this->MAMAL->cheetah()){
					redirect('UNIVERSE/BimaSakti');
				}
				$week = $this->Laporan_model->selectWeek();
				foreach ($week as $g)
				{
					$data['id'][]	 	    = $g->id;
					$data['intid_week'][] 	= $g->intid_week;
				}
				$data['sidebar'] = $this->REPTIL->Snake($id);
				$data['key'] = $id;
				$this->load->view('admin_views/universe/form_keuangan_mingguan',$data);
				//
				//echo "helo ".$id;
			}
		}
		function mars($id){
			if($_POST){
				if($this->input->post('week')){
					$data['intid_week'] = $this->input->post('week');
					$data['default'] = $this->BIRD->WoodPecker_Melanerpes($this->input->post('week'));
					//echo $data['default'];
					$this->load->view('admin_views/universe/page_keuangan_mingguan_omset',$data);
				}else{
					$data['intbulan'] = $this->input->post('month');
					$data['default'] = $this->BIRD->WoodPecker_Sphyrapicus($this->input->post('month'));
					//echo $data['default'];
					$this->load->view('admin_views/universe/page_keuangan_bulanan_omset',$data);
				}
			}else{
				if($id != $this->MAMAL->cheetah()){
					redirect('UNIVERSE/BimaSakti');
				}
				$week = $this->Laporan_model->selectWeek();
				foreach ($week as $g)
				{
					$data['id'][]	 	    = $g->id;
					$data['intid_week'][] 	= $g->intid_week;
				}
				$data['sidebar'] = $this->REPTIL->Snake($id);
				$data['key'] = $id;
				$this->load->view('admin_views/universe/form_omset_mingguan_all',$data);
			}
		}*/
		function mars($id){
			if($_POST){
				if($this->input->post('week')){
					$data['intid_week'] = $this->input->post('week');
					$data['default'] = $this->BIRD->Grey_Headed_Woodpecker($this->input->post('week'));
					//echo $data['default'];
					$this->load->view('admin_views/universe/page_keuangan_mingguan_omset',$data);
				}else{
					$data['intbulan'] = $this->input->post('month');
					$data['default'] = $this->BIRD->Black_WoodPecker($this->input->post('month'));
					//echo $data['default'];
					$this->load->view('admin_views/universe/page_keuangan_bulanan_omset',$data);
					//echo "hello";
				}
			}else{
				if($id != $this->MAMAL->cheetah()){
					redirect('UNIVERSE/BimaSakti');
				}
				$week = $this->Laporan_model->selectWeek();
				foreach ($week as $g)
				{
					$data['id'][]	 	    = $g->id;
					$data['intid_week'][] 	= $g->intid_week;
				}
				$data['sidebar'] = $this->REPTIL->Snake($id);
				$data['key'] = $id;
				$this->load->view('admin_views/universe/form_omset_mingguan_all',$data);
			}
		}
		function lgwithweek(){
			if (!$_POST)
			{
				$week = $this->Laporan_model->selectWeek();
				foreach ($week as $g)
				{
					$data['id'][]	 	= $g->id;
					$data['intid_week'][] 	= $g->intid_week;
				}
				$cabang = $this->User_model->getCabang($this->session->userdata('username'));
				$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);
	
				$data['cabang'] = $nm_cabang[0]->strnama_cabang;
				$data['id_cabang'] = $nm_cabang[0]->intid_cabang;
				$data['id_wilayah'] = $nm_cabang[0]->intid_wilayah;
	
				$data['user'] = $this->session->userdata('username');
				$data['max_id'] = $this->getno_nota();
				
				$this->load->view('admin_views/penjualan/nota_lg_universe', $data);
			 }else{
			 
				$this->Penjualan_model->insertNota($_POST);
				
				//ini penting bwt dirubah
				/*
				$r = $this->Penjualan_model->cekNota001($this->input->post('strkode_dealer'), $this->input->post('id_unit'), $this->input->post('totallg001'));
				for($i=0;$i<=sizeof($r);$i++){ 
					if(!empty($r[$i]->intid_nota)){	
						$this->Penjualan_model->updateNotaLg($r[$i]->intid_nota);
					}
				}
				*/
			$pilih = $this->input->post('pilih');
			$nomor_nota = $this->input->post('nomor_nota');
			for($i = 0; $i < sizeof($pilih);$i++){
				//echo ">>".$nomor_nota[$pilih[$i]]."<br />";
				if(!empty($nomor_nota[$pilih[$i]])){	
						$this->Penjualan_model->updateNotaLg_intno_nota($nomor_nota[$pilih[$i]]);
				}
			}
			 
	/*______________________________________________________________________
	|									|
	|									|
	|									|
	|									|
	|			End of Kode Baru				|
	|									|
	|									|
	|									|
	|______________________________________________________________________*/
							
				$max = $this->Penjualan_model->get_MaxNota()->result();
				$id = $max[0]->intid_nota;
				$data = $this->input->post('barang');
				for($i=1;$i<=sizeof($data);$i++){
				if(!empty($data[$i]['intid_id'])){
				$detail = array(
									'intid_nota' 			=> $id,
									'intid_barang'	        => $data[$i]['intid_id'],
									'intquantity'		    => $data[$i]['intquantity'],
									'intid_harga'		    => $data[$i]['intid_id'],
									'intharga'				=> $data[$i]['intid_harga']
									);
	
				 $this->Penjualan_model->add($detail);
				 $this->Penjualan_model->addStok($data[$i]['intid_id'], $data[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				}
				}
	
				 $data_free = $this->input->post('barang_free');
				 if(sizeof($data_free) <= 1){
					 $d = sizeof($data_free) + 1;
				 }else{
					 $d = sizeof($data_free);
				 }
				 for($i=1;$i<=30;$i++){
				 
				
				 if(!empty($data_free[$i]['intid_id'])){
				 
				  
				 $detail_free = array(
								'intid_nota' 			=> $id,
								'intid_barang'	        => $data_free[$i]['intid_id'],
								'intquantity'		    => $data_free[$i]['intquantity'],
								'is_free' => 1
								);
				 $this->Penjualan_model->add($detail_free);
				 $this->Penjualan_model->addStok($data_free[$i]['intid_id'], $data_free[$i]['intquantity'], $this->input->post('intid_cabang'), $this->input->post('intno_nota'));
				 }
				   
			 }
				redirect('penjualan/cetak_notalg');
			}
	}
	function getno_nota(){
        $cabang = $this->User_model->getCabang($this->session->userdata('username'));
        $week = $this->Penjualan_model->selectWeek();
        /*$max = $this->Penjualan_model->get_MaxNota()->result();
        $getnota = $this->Penjualan_model->getNoNota($max[0]->intid_nota);
		if(empty($getnota[0]->intno_nota)){
			$nilai =1;
		} else {
			$nilai = $getnota[0]->intno_nota;
		}*/

        $getnota = $this->Penjualan_model->getNoNotaNew();
		$nilai = $getnota[0]->id;
		$id = $nilai + 1;
		$this->Penjualan_model->getNoNotaUpdate($id);
		//$noUrut = (int) substr($nilai, 6, 9);
        //$noUrut++;
        //$kode = $cabang[0]->intkode_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $noUrut);
		$kode = $cabang[0]->intid_cabang.".".$week[0]->intid_week.".".sprintf("%05s", $nilai);
        return $kode;

    }
	function check_omset_universe() {
                $cabang = $this->User_model->getCabang($this->session->userdata('username'));
           		$nm_cabang = $this->Cabang_model->select($cabang[0]->intid_cabang);

            	$id_cabang = $nm_cabang[0]->intid_cabang;
				$id = $this->Penjualan_model->getidDealer($this->input->post('strkode_dealer'));
				//$data['omset'] = $this->Penjualan_model->getOmset($id[0]->intid_dealer, $id_cabang);
				$data['omset'] = $this->BIRD->owl($id[0]->intid_dealer,$this->input->post('week'));
				
				$this->load->view('admin_views/penjualan/omset', $data);
    }
	function show(){
		$query = $this->BIRD->show_temp();
		foreach($query as $row){
			echo "insert into stok values('','5990','".$row->intid_cabang."','21','0','0','0','0','0','2013-05-28');<br />";
		}
	}
		function venus($id){
				if($_POST){
					//
					$data['intid_jpenjualan'] = $this->input->post('intid_jpenjualan');
					$data['bulan_pertama'] = $this->input->post('bulan_pertama');
					$data['bulan_kedua'] = $this->input->post('bulan_kedua');
					$data['query']	=	$this->BIRD->Golden_Eagle($data['intid_jpenjualan'],$data['bulan_pertama'],$data['bulan_kedua']);
					$namaPenjualan = $this->MAMAL->monkey('select strnama_jpenjualan from jenis_penjualan where intid_jpenjualan ='.$data['intid_jpenjualan'].'');
					$data['strnama_jpenjualan'] = $namaPenjualan[0]->strnama_jpenjualan;
					$data['bulan1'] = $data['bulan_pertama'];
					$data['bulan2'] = $data['bulan_kedua'];
					
					$this->load->view('admin_views/universe/page_penjualan_omset_unit',$data);
					}else{
						if($id != $this->MAMAL->cheetah()){
							redirect('UNIVERSE/BimaSakti');
							}
						$jpenjualan = $this->Penjualan_model->selectJPenjualan();
						foreach ($jpenjualan as $g)
						{
							$data['intid_jpenjualan'][]	 	= $g->intid_jpenjualan;
							$data['strnama_jpenjualan'][] 	= $g->strnama_jpenjualan;
							}
						$data['sidebar'] = $this->REPTIL->Snake($id);
						$data['select_bulan']	=	$this->REPTIL->lizard();
						$data['key'] = $id;
						$this->load->view('admin_views/universe/form_penjualan_omset_unit',$data);
						}
			}
			//POST dari earth
			function cetak_mingguan()
			{
			
			
			if ($this->input->post('intid_jpenjualan')==10)
			{ 
				$data['default'] = $this->BIRD->get_CetakPenjualanSKMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'));
				$this->load->view('halaman/excel_3', $data);
			
			} else if ($this->input->post('intid_jpenjualan')==9)
			{ 
				$data['default'] = $this->BIRD->get_CetakPenjualanArisanMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'), $this->input->post('intid_jpenjualan'));
				$this->load->view('halaman/excel_2', $data);
			} else {
				$data['default'] = $this->BIRD->get_CetakPenjualanMingguan($this->input->post('intid_week'),$this->input->post('intid_cabang'), $this->input->post('intid_jpenjualan'));
				$this->load->view('halaman/excel_1', $data);
			
			}
		}
	}
?>